<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class UserController extends Controller
{
    //账户设置
    public function user_set()
    {
    	return view('home.user.user_set');
    }
    //执行更新
    public function update(Request $request)
    {


    	// return dd($request -> input());
    	// 判断
    	$this -> validate($request,[
    			'username' => 'required|min:6|max:18'
    		],[
    			'username.required' => '用户名不能为空',
    			'username.min' => '用户名最少6位',
    			'username.max' => '用户名最长18位'    			
    		]);
    	// 准备数据    	
    	$data = $request -> except('_token');

    		if ($request -> hasFile('img_u')) 
    		{
	    		if ($request -> file('img_u') -> isValid()) 
	    		{
	    			//移动文件
	    			$suffix = $request -> file('img_u') -> getClientOriginalExtension();
	    			//随机一个文件名称
	    			$fileName = time().mt_rand(100000,999999);
	    			//移动文件
	    			$request -> file('img_u') -> move('./uploads', $fileName.'.'.$suffix);
	                
	    		
	                $data['img_u'] = $fileName.'.'.$suffix;
	    		}
	    	}

    	//执行更新
    	$res = DB::table('users') -> where('email', session('master') -> email) -> update($data);
    	// 判断
    	if($res)
    	{    		
    		$data1 =DB::table('users') -> where('email', session('master') -> email) -> first();
    		session(['master' => $data1]);
    		return back() -> with(['info' => '保存成功']);
    	}else
    	{
    		return back() -> with(['info' => '保存失败,请重新保存']);
    	}
    	
    }
    //退出登录
    public function logout()
    {
        session() -> forget('master');
        return redirect('/home/login');
    }
    //退出登录
    public function layoutlogout()
    {
    	session() -> forget('master');
    	return redirect('/');
    }
}
